import Taro from '@tarojs/taro'
import app from '../../../main.js'
let myJs = {
    getData: function() {
        this.state = {
            isOpened: false,
            isOpened2: false,
            isOpened3: false,
            text: '',
            tempImg: '',
            tempFiles: [],
            message: '',
            images: [],
            case_detail: {
                id: '',
                physical_images: ''
            }
        };
        return this.state;
    },

    updateCase_detail() {
        var me = this;
        me.state.case_detail.physical_images = JSON.stringify(me.state.images);
        app.post("api/case_details/update", me.state.case_detail, function(res) {
            app.navigateBack('../editCase/editCase?id=' + me.state.case_detail.id);
        });
    },
    getCase_detail() {
        var me = this;
        app.get("api/case_details/get", {id: me.state.case_detail.id}, function(res) {
            var images = [];
            if(res.data.result.physical_images && res.data.result.physical_images != 'null') {
                images = JSON.parse(res.data.result.physical_images);
            }
            me.setState({
                case_detail: res.data.result,
                images: images
            });
        });
    },
    imageChange(files, type) {
        if(type == 'add') {
            this.state.tempImg = files[files.length - 1]['url'];
            this.setState({
                isOpened: true,
                text: ''
            });
        } else {
            // this.state.tempFile = files;
            this.setState({
                images: files
            });
        }
    },

    comfirmModal() {
        var me = this;
        if(!this.state.text) {
            this.showToast('请输入照片名称');
            return;
        }

        Taro.showLoading({
            title: '上传中...',
            mask: true
        })

        wx.uploadFile({
            url: app.apiUri + '/admin/upload/image',
            filePath: me.state.tempImg,
            name: 'file',
            formData: {
              name: 'file'
            },
            success(res) {
              Taro.hideLoading();
              if (res.statusCode == 413) {
                app.toast('文件过大，请重新上传');
                return;
              }
              let data = JSON.parse(res.data);
              if (data.status == 200) {
                me.state.images.push({url: app.getPath(data.result), text: me.state.text});
                me.setState({
                    images: me.state.images,
                    isOpened: false,
                    tempImg: ''
                });
              }
            }
          })
    },
    componentWillMount () {
        this.init();
    },
    navigateTo(url) {
        app.navigateTo(url);
    },
    
    handleDetailChange(name, event) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }

        if(names.length > 1) {
            temp[names[i]] = event.detail.value;
            this.setState({n: temp});
        } else {
            this.setState({n: event.detail.value});
        }
    },
    handleChange(name, v) {
        var names = name.split('.');
        var n = names[0];
        var temp = this.state;
        for(var i = 0; i < names.length - 1; i++) {
            temp = temp[names[i]];
        }
        
        temp[names[i]] = v;
        this.setState({n: temp});
    },
    redirectTo(url) {
        app.redirectTo(url);
    },
    componentDidMount () {},
    componentWillUnmount () {},
    componentDidHide () {}
}
export default myJs;
